const express = require("express")
const router = express.Router()
// let db = require("../mysql/db")

const source = require("../source/list")


/**
 * GET /api/v1/list
 * @summary 获取所有商品
 * @description 这是一个获取所有商品接口
 * @tags [商品管理]
 * @return {array<object>} 200 - 成功获取所有商品列表
 */
 router.get('/api/v1/list',async(req,res,next)=>{
    res.json({code:200,data:source.list,message:"数据获取成功"})
    next();
})


/**
 * POST /api/v1/search
 * @summary 接口搜索
 * @description 根据商品名称搜索
 * @tags [商品管理]
 * @produces application/json
 * @param {object} request.body.required - 商品信息
 * @return {object} 201 - 获取成功
 * @example request - 示例请求
 * {
 *   "name": "憨憨狗"
 * }
 * @example response - 示例响应
 * {
 *   "id": 3,
 *   "name": "憨憨狗",
 *   "age": 30
 * }
 * @consumes application/json
 * @return {array<object>} 200 - 成功获取商品名称搜索
 */
router.post('/api/v1/search',async(req,res,next)=>{
  const { name, age } = req.body;
  const list0 = source.list.filter((item)=>{
   return item.name.includes(name)
  })
  res.json({code:200,data:list0,message:"数据获取成功"})
  next();
})


/**
 * POST /api/v1/getById
 * @summary 根据商品id获取商品详情22222
 * @description 根据商品id获取商品详情
 * @tags [商品管理]
 * @produces application/json
 * @param {object} request.body.required - 商品id
 * @return {object} 201 - 成功获取商品
 * @example request - 示例请求
 * {
 *   "id": 1
 * }
 * @example response - 示例响应
 * {
 *   "id": 3,
 *   "name": "憨憨狗",
 *   "age": 30
 * }
 * @consumes application/json
 * @return {array<object>} 200 - 成功获取商品
 */
router.post('/api/v1/getById',async(req,res,next)=>{
  const { name, id } = req.body;
  const list0 = source.list.filter((item)=>{
   return item.id===id
  })
  res.json({code:200,data:list0,message:"数据获取成功"})
  next();
})

module.exports = router;